package com.ynny.chapter05_02.service.impl;

import com.ynny.chapter05_02.entity.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl {


    // 读的操作
    // value :   缓存名称  最外层文件夹的名称
    // key :  缓存key  传入的参数
    // 判断 里面 有没有缓存  userList::#id
    @Cacheable(value = "testCache#60" , key = "'userList' + #id " )
    public List<User> getAllUsers(int id){

        //模拟查询数据库
        ArrayList<User> users = new ArrayList<>();

        for (int i = 0; i < 50; i++) {
            users.add(new User(i,"张三"));

        }

        System.out.println("数据加载~~~~~" + "userList::" + id);

        return users;

    }


    @CacheEvict(value = "testCache" , key = "'userList' + #user.id ")
    public void editUser(User user){

        System.out.println("数据修改~~~~~" + "userList::" + user.getId());

    }

    @CacheEvict(value = "testCache", allEntries = true)  // 清空整个缓存
    public void batchUpdateUsers() {
        System.out.println("清空缓存 key ：：" + "testCache");
    }


}
